/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved.
 */
package com.icinfo.cloud.provider.punish.service.impl;

import com.icinfo.cloud.provider.punish.config.dto.RelationWorkflowInfoRequest;
import com.icinfo.cloud.provider.punish.config.model.ConfigBusiParamMain;
import com.icinfo.cloud.provider.punish.mapper.ConfigBusiParamMainMapper;
import com.icinfo.cloud.provider.punish.service.IConfigBusiParamMainService;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 描述: 业务参数配置主表 config_busi_param_main 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2022年02月22日
 */
@Service
public class ConfigBusiParamMainServiceImpl extends MyBatisServiceSupport implements IConfigBusiParamMainService {
    /**
     * 日志记录器
     */
    private static final Logger logger = LoggerFactory.getLogger(ConfigBusiParamMainServiceImpl.class);

    @Autowired
    private ConfigBusiParamMainMapper configBusiParamMainMapper;

    /**
     * 根据领域、区划获取
     * @author liyafeng
     * @date 2022/3/13
      * @param
     * @return {@link List< ConfigBusiParamMain>}
     */
    @Override
    public List<ConfigBusiParamMain> getConfigBusiParamMainList(RelationWorkflowInfoRequest request) {
        List<ConfigBusiParamMain> configBusiParamMainList=configBusiParamMainMapper.selectRelationWorkflowInfoId(request);
        if(null!=configBusiParamMainList&&configBusiParamMainList.size()>0){
            return configBusiParamMainList;
        }
        String areaCode=request.getApplyAreaCode();
        //若为地市 则查省级
        if(areaCode.endsWith("00000000")){
            //查省级
            String sAreaCode=areaCode.substring(0,2)+"00"+areaCode.substring(4,areaCode.length());
            RelationWorkflowInfoRequest relationWorkflowInfoRequest=new RelationWorkflowInfoRequest();
            relationWorkflowInfoRequest.setApplyAreaCode(sAreaCode);
            relationWorkflowInfoRequest.setApplyFieldCode(request.getApplyFieldCode());
            configBusiParamMainList=configBusiParamMainMapper.selectRelationWorkflowInfoId(relationWorkflowInfoRequest);
            if(null!=configBusiParamMainList&&configBusiParamMainList.size()>0){
                return configBusiParamMainList;
            }
        //若为区县 则查地市 省级
        }else if(areaCode.endsWith("000000")){
            //查地市
            String dAreaCode=areaCode.substring(0,4)+"00"+areaCode.substring(6,areaCode.length());
            RelationWorkflowInfoRequest relationWorkflowInfoRequest=new RelationWorkflowInfoRequest();
            relationWorkflowInfoRequest.setApplyAreaCode(dAreaCode);
            relationWorkflowInfoRequest.setApplyFieldCode(request.getApplyFieldCode());
            configBusiParamMainList=configBusiParamMainMapper.selectRelationWorkflowInfoId(relationWorkflowInfoRequest);
            if(null!=configBusiParamMainList&&configBusiParamMainList.size()>0){
                return configBusiParamMainList;
            }
            //查省级
            String sAreaCode=areaCode.substring(0,2)+"0000"+areaCode.substring(6,areaCode.length());
            RelationWorkflowInfoRequest sRelationWorkflowInfoRequest=new RelationWorkflowInfoRequest();
            sRelationWorkflowInfoRequest.setApplyAreaCode(sAreaCode);
            sRelationWorkflowInfoRequest.setApplyFieldCode(request.getApplyFieldCode());
            configBusiParamMainList=configBusiParamMainMapper.selectRelationWorkflowInfoId(sRelationWorkflowInfoRequest);
            if(null!=configBusiParamMainList&&configBusiParamMainList.size()>0){
                return configBusiParamMainList;
            }
        //街道乡镇 则查区县 地市 省级
        }else if(areaCode.endsWith("000")){
            //区县
            String qAreaCode=areaCode.substring(0,6)+"000"+areaCode.substring(9,areaCode.length());
            RelationWorkflowInfoRequest qRelationWorkflowInfoRequest=new RelationWorkflowInfoRequest();
            qRelationWorkflowInfoRequest.setApplyAreaCode(qAreaCode);
            qRelationWorkflowInfoRequest.setApplyFieldCode(request.getApplyFieldCode());
            configBusiParamMainList=configBusiParamMainMapper.selectRelationWorkflowInfoId(qRelationWorkflowInfoRequest);
            if(null!=configBusiParamMainList&&configBusiParamMainList.size()>0){
                return configBusiParamMainList;
            }
            //地市
            String dAreaCode=areaCode.substring(0,4)+"00000"+areaCode.substring(9,areaCode.length());
            RelationWorkflowInfoRequest dRelationWorkflowInfoRequest=new RelationWorkflowInfoRequest();
            dRelationWorkflowInfoRequest.setApplyAreaCode(dAreaCode);
            dRelationWorkflowInfoRequest.setApplyFieldCode(request.getApplyFieldCode());
            configBusiParamMainList=configBusiParamMainMapper.selectRelationWorkflowInfoId(dRelationWorkflowInfoRequest);
            if(null!=configBusiParamMainList&&configBusiParamMainList.size()>0){
                return configBusiParamMainList;
            }
            //省级
            String sAreaCode=areaCode.substring(0,2)+"0000000"+areaCode.substring(9,areaCode.length());
            RelationWorkflowInfoRequest sRelationWorkflowInfoRequest=new RelationWorkflowInfoRequest();
            sRelationWorkflowInfoRequest.setApplyAreaCode(sAreaCode);
            sRelationWorkflowInfoRequest.setApplyFieldCode(request.getApplyFieldCode());
            configBusiParamMainList=configBusiParamMainMapper.selectRelationWorkflowInfoId(sRelationWorkflowInfoRequest);
            if(null!=configBusiParamMainList&&configBusiParamMainList.size()>0){
                return configBusiParamMainList;
            }
        }
        configBusiParamMainList=configBusiParamMainMapper.selectAllFieldAndAllProvinceRelationWorkflowInfoId();
        return configBusiParamMainList;
    }

}
